using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml.Xsl;

namespace Eldorado.Wizard
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form, Eldorado.Metadata.IXmlWriterProvider
	{
		private System.Windows.Forms.Panel panelBottom;
		private System.Windows.Forms.Panel panelTop;
		private System.Windows.Forms.Panel panelBody;
		private System.Windows.Forms.Button buttonCancel;
		private System.Windows.Forms.Button buttonPrev;
		private System.Windows.Forms.Button buttonNext;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button buttonLoadProject;
		private System.Windows.Forms.TextBox textBoxXmiFilePath;
		private System.Windows.Forms.Button buttonSelectInputXMI;
		private DataSet dsProject;

		private Node xmiNode;
		private Node mappingsNode;
		private Node codeNode;
		private Node dbNode;

		private string CSharpInput = "CSharpInput.xslt";
		private string MySqlInput = "MySqlInput.xslt";
		private string MsSqlInput = "MsSqlInput.xslt";
		private string MapInput = "MapInput.xslt";
		private string XMIInput = "XMIInput.xslt";
		private string DBInput;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private int selectedTabIndex;
		private bool stepsReady;

		private string outputPath = "";
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.DataGrid dataGridUMDDataTypes;
		private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.TabPage tabPage4;
		private System.Windows.Forms.TabPage tabPage5;
		private System.Windows.Forms.TabPage tabPage6;
		private System.Windows.Forms.TabControl tabControlMapping;
		private System.Windows.Forms.DataGrid dataGridCodeValues;
		private System.Windows.Forms.DataGrid dataGridDBValues;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label labelGenerating;
		private System.Windows.Forms.Button buttonSave;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBoxOutput;
		private System.Windows.Forms.Button buttonChooseOutput;
		private System.Windows.Forms.RadioButton radioButtonMsSql;
		private System.Windows.Forms.RadioButton radioButtonMySql;
		private FormResults results;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			stepsReady = false;
			InitializeComponent();
			selectedTabIndex = 0;
			stepsReady = true;

			dsProject = new DataSet();

			results = new FormResults();
			textBoxOutput.Text = "c:\\temp";
			foreach(System.Reflection.AssemblyName assName in typeof(Form1).Assembly.GetReferencedAssemblies())
			{
				AppDomain.CurrentDomain.Load(assName);
			}

			xmiNode = new Node();
			mappingsNode = new Node();
			codeNode = new Node();
			dbNode = new Node();

			currentNode = xmiNode;
			DBInput = MsSqlInput;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.panelBottom = new System.Windows.Forms.Panel();
			this.buttonSave = new System.Windows.Forms.Button();
			this.buttonNext = new System.Windows.Forms.Button();
			this.buttonPrev = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.panelTop = new System.Windows.Forms.Panel();
			this.panelBody = new System.Windows.Forms.Panel();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.buttonChooseOutput = new System.Windows.Forms.Button();
			this.textBoxOutput = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButtonMySql = new System.Windows.Forms.RadioButton();
			this.radioButtonMsSql = new System.Windows.Forms.RadioButton();
			this.buttonSelectInputXMI = new System.Windows.Forms.Button();
			this.textBoxXmiFilePath = new System.Windows.Forms.TextBox();
			this.buttonLoadProject = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.dataGridUMDDataTypes = new System.Windows.Forms.DataGrid();
			this.panel1 = new System.Windows.Forms.Panel();
			this.label2 = new System.Windows.Forms.Label();
			this.tabPage3 = new System.Windows.Forms.TabPage();
			this.tabControlMapping = new System.Windows.Forms.TabControl();
			this.tabPage4 = new System.Windows.Forms.TabPage();
			this.dataGridCodeValues = new System.Windows.Forms.DataGrid();
			this.tabPage5 = new System.Windows.Forms.TabPage();
			this.dataGridDBValues = new System.Windows.Forms.DataGrid();
			this.tabPage6 = new System.Windows.Forms.TabPage();
			this.button1 = new System.Windows.Forms.Button();
			this.labelGenerating = new System.Windows.Forms.Label();
			this.panelBottom.SuspendLayout();
			this.panelBody.SuspendLayout();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridUMDDataTypes)).BeginInit();
			this.panel1.SuspendLayout();
			this.tabPage3.SuspendLayout();
			this.tabPage4.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridCodeValues)).BeginInit();
			this.tabPage5.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridDBValues)).BeginInit();
			this.tabPage6.SuspendLayout();
			this.SuspendLayout();
			// 
			// panelBottom
			// 
			this.panelBottom.Controls.Add(this.buttonSave);
			this.panelBottom.Controls.Add(this.buttonNext);
			this.panelBottom.Controls.Add(this.buttonPrev);
			this.panelBottom.Controls.Add(this.buttonCancel);
			this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panelBottom.DockPadding.Bottom = 7;
			this.panelBottom.DockPadding.Left = 5;
			this.panelBottom.DockPadding.Right = 20;
			this.panelBottom.DockPadding.Top = 7;
			this.panelBottom.Location = new System.Drawing.Point(0, 309);
			this.panelBottom.Name = "panelBottom";
			this.panelBottom.Size = new System.Drawing.Size(488, 40);
			this.panelBottom.TabIndex = 0;
			// 
			// buttonSave
			// 
			this.buttonSave.Location = new System.Drawing.Point(16, 8);
			this.buttonSave.Name = "buttonSave";
			this.buttonSave.Size = new System.Drawing.Size(96, 24);
			this.buttonSave.TabIndex = 3;
			this.buttonSave.Text = "Save project";
			this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
			// 
			// buttonNext
			// 
			this.buttonNext.Location = new System.Drawing.Point(256, 8);
			this.buttonNext.Name = "buttonNext";
			this.buttonNext.TabIndex = 2;
			this.buttonNext.Text = "Next";
			this.buttonNext.Click += new System.EventHandler(this.buttonNext_Click);
			// 
			// buttonPrev
			// 
			this.buttonPrev.Location = new System.Drawing.Point(168, 8);
			this.buttonPrev.Name = "buttonPrev";
			this.buttonPrev.TabIndex = 1;
			this.buttonPrev.Text = "Prev";
			this.buttonPrev.Click += new System.EventHandler(this.buttonPrev_Click);
			// 
			// buttonCancel
			// 
			this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.buttonCancel.Location = new System.Drawing.Point(392, 8);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 0;
			this.buttonCancel.Text = "Cancel";
			// 
			// panelTop
			// 
			this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
			this.panelTop.Location = new System.Drawing.Point(0, 0);
			this.panelTop.Name = "panelTop";
			this.panelTop.Size = new System.Drawing.Size(488, 48);
			this.panelTop.TabIndex = 1;
			// 
			// panelBody
			// 
			this.panelBody.Controls.Add(this.tabControl1);
			this.panelBody.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panelBody.Location = new System.Drawing.Point(0, 48);
			this.panelBody.Name = "panelBody";
			this.panelBody.Size = new System.Drawing.Size(488, 261);
			this.panelBody.TabIndex = 2;
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Controls.Add(this.tabPage3);
			this.tabControl1.Controls.Add(this.tabPage4);
			this.tabControl1.Controls.Add(this.tabPage5);
			this.tabControl1.Controls.Add(this.tabPage6);
			this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabControl1.ItemSize = new System.Drawing.Size(58, 0);
			this.tabControl1.Location = new System.Drawing.Point(0, 0);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(488, 261);
			this.tabControl1.TabIndex = 0;
			this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.buttonChooseOutput);
			this.tabPage1.Controls.Add(this.textBoxOutput);
			this.tabPage1.Controls.Add(this.label3);
			this.tabPage1.Controls.Add(this.groupBox1);
			this.tabPage1.Controls.Add(this.buttonSelectInputXMI);
			this.tabPage1.Controls.Add(this.textBoxXmiFilePath);
			this.tabPage1.Controls.Add(this.buttonLoadProject);
			this.tabPage1.Controls.Add(this.label1);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(480, 235);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Init";
			// 
			// buttonChooseOutput
			// 
			this.buttonChooseOutput.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.buttonChooseOutput.Location = new System.Drawing.Point(448, 80);
			this.buttonChooseOutput.Name = "buttonChooseOutput";
			this.buttonChooseOutput.Size = new System.Drawing.Size(24, 20);
			this.buttonChooseOutput.TabIndex = 7;
			this.buttonChooseOutput.Text = "...";
			this.buttonChooseOutput.Click += new System.EventHandler(this.buttonChooseOutput_Click);
			// 
			// textBoxOutput
			// 
			this.textBoxOutput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxOutput.Location = new System.Drawing.Point(136, 80);
			this.textBoxOutput.Name = "textBoxOutput";
			this.textBoxOutput.Size = new System.Drawing.Size(312, 20);
			this.textBoxOutput.TabIndex = 6;
			this.textBoxOutput.Text = "";
			this.textBoxOutput.TextChanged += new System.EventHandler(this.textBoxOutput_TextChanged);
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(16, 82);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(69, 16);
			this.label3.TabIndex = 5;
			this.label3.Text = "Output path :";
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Controls.Add(this.radioButtonMySql);
			this.groupBox1.Controls.Add(this.radioButtonMsSql);
			this.groupBox1.Location = new System.Drawing.Point(8, 104);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(464, 48);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Database type";
			// 
			// radioButtonMySql
			// 
			this.radioButtonMySql.Location = new System.Drawing.Point(152, 16);
			this.radioButtonMySql.Name = "radioButtonMySql";
			this.radioButtonMySql.TabIndex = 1;
			this.radioButtonMySql.Text = "MySql";
			// 
			// radioButtonMsSql
			// 
			this.radioButtonMsSql.Checked = true;
			this.radioButtonMsSql.Location = new System.Drawing.Point(32, 16);
			this.radioButtonMsSql.Name = "radioButtonMsSql";
			this.radioButtonMsSql.TabIndex = 0;
			this.radioButtonMsSql.TabStop = true;
			this.radioButtonMsSql.Text = "MsSql Server";
			// 
			// buttonSelectInputXMI
			// 
			this.buttonSelectInputXMI.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.buttonSelectInputXMI.Location = new System.Drawing.Point(448, 46);
			this.buttonSelectInputXMI.Name = "buttonSelectInputXMI";
			this.buttonSelectInputXMI.Size = new System.Drawing.Size(24, 20);
			this.buttonSelectInputXMI.TabIndex = 3;
			this.buttonSelectInputXMI.Text = "...";
			this.buttonSelectInputXMI.Click += new System.EventHandler(this.buttonSelectInputXMI_Click);
			// 
			// textBoxXmiFilePath
			// 
			this.textBoxXmiFilePath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxXmiFilePath.Location = new System.Drawing.Point(136, 46);
			this.textBoxXmiFilePath.Name = "textBoxXmiFilePath";
			this.textBoxXmiFilePath.Size = new System.Drawing.Size(312, 20);
			this.textBoxXmiFilePath.TabIndex = 2;
			this.textBoxXmiFilePath.Text = "";
			// 
			// buttonLoadProject
			// 
			this.buttonLoadProject.Location = new System.Drawing.Point(16, 8);
			this.buttonLoadProject.Name = "buttonLoadProject";
			this.buttonLoadProject.Size = new System.Drawing.Size(88, 24);
			this.buttonLoadProject.TabIndex = 1;
			this.buttonLoadProject.Text = "Load Project";
			this.buttonLoadProject.Click += new System.EventHandler(this.buttonLoadProject_Click);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(16, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(108, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Select xmi input file :";
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.dataGridUMDDataTypes);
			this.tabPage2.Controls.Add(this.panel1);
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(480, 235);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "Uml data mapping";
			// 
			// dataGridUMDDataTypes
			// 
			this.dataGridUMDDataTypes.DataMember = "";
			this.dataGridUMDDataTypes.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataGridUMDDataTypes.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridUMDDataTypes.Location = new System.Drawing.Point(0, 32);
			this.dataGridUMDDataTypes.Name = "dataGridUMDDataTypes";
			this.dataGridUMDDataTypes.Size = new System.Drawing.Size(480, 203);
			this.dataGridUMDDataTypes.TabIndex = 1;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.label2);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(480, 32);
			this.panel1.TabIndex = 0;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 23);
			this.label2.TabIndex = 0;
			this.label2.Text = "UML Data types mapping";
			// 
			// tabPage3
			// 
			this.tabPage3.Controls.Add(this.tabControlMapping);
			this.tabPage3.Location = new System.Drawing.Point(4, 22);
			this.tabPage3.Name = "tabPage3";
			this.tabPage3.Size = new System.Drawing.Size(480, 235);
			this.tabPage3.TabIndex = 2;
			this.tabPage3.Text = "Pivot Unique Ids";
			// 
			// tabControlMapping
			// 
			this.tabControlMapping.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabControlMapping.Location = new System.Drawing.Point(0, 0);
			this.tabControlMapping.Name = "tabControlMapping";
			this.tabControlMapping.SelectedIndex = 0;
			this.tabControlMapping.Size = new System.Drawing.Size(480, 235);
			this.tabControlMapping.TabIndex = 0;
			// 
			// tabPage4
			// 
			this.tabPage4.Controls.Add(this.dataGridCodeValues);
			this.tabPage4.Location = new System.Drawing.Point(4, 22);
			this.tabPage4.Name = "tabPage4";
			this.tabPage4.Size = new System.Drawing.Size(480, 235);
			this.tabPage4.TabIndex = 3;
			this.tabPage4.Text = "Code default values";
			// 
			// dataGridCodeValues
			// 
			this.dataGridCodeValues.DataMember = "";
			this.dataGridCodeValues.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataGridCodeValues.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridCodeValues.Location = new System.Drawing.Point(0, 0);
			this.dataGridCodeValues.Name = "dataGridCodeValues";
			this.dataGridCodeValues.Size = new System.Drawing.Size(480, 235);
			this.dataGridCodeValues.TabIndex = 2;
			// 
			// tabPage5
			// 
			this.tabPage5.Controls.Add(this.dataGridDBValues);
			this.tabPage5.Location = new System.Drawing.Point(4, 22);
			this.tabPage5.Name = "tabPage5";
			this.tabPage5.Size = new System.Drawing.Size(480, 235);
			this.tabPage5.TabIndex = 4;
			this.tabPage5.Text = "db Default values";
			// 
			// dataGridDBValues
			// 
			this.dataGridDBValues.DataMember = "";
			this.dataGridDBValues.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataGridDBValues.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridDBValues.Location = new System.Drawing.Point(0, 0);
			this.dataGridDBValues.Name = "dataGridDBValues";
			this.dataGridDBValues.Size = new System.Drawing.Size(480, 235);
			this.dataGridDBValues.TabIndex = 2;
			// 
			// tabPage6
			// 
			this.tabPage6.Controls.Add(this.button1);
			this.tabPage6.Controls.Add(this.labelGenerating);
			this.tabPage6.Location = new System.Drawing.Point(4, 22);
			this.tabPage6.Name = "tabPage6";
			this.tabPage6.Size = new System.Drawing.Size(480, 235);
			this.tabPage6.TabIndex = 5;
			this.tabPage6.Text = "Generating";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 32);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(88, 24);
			this.button1.TabIndex = 1;
			this.button1.Text = "Display results";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// labelGenerating
			// 
			this.labelGenerating.Location = new System.Drawing.Point(16, 12);
			this.labelGenerating.Name = "labelGenerating";
			this.labelGenerating.Size = new System.Drawing.Size(136, 16);
			this.labelGenerating.TabIndex = 0;
			this.labelGenerating.Text = "Generating files ....";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(488, 349);
			this.Controls.Add(this.panelBody);
			this.Controls.Add(this.panelTop);
			this.Controls.Add(this.panelBottom);
			this.Name = "Form1";
			this.Text = "Form1";
			this.panelBottom.ResumeLayout(false);
			this.panelBody.ResumeLayout(false);
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridUMDDataTypes)).EndInit();
			this.panel1.ResumeLayout(false);
			this.tabPage3.ResumeLayout(false);
			this.tabPage4.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridCodeValues)).EndInit();
			this.tabPage5.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridDBValues)).EndInit();
			this.tabPage6.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void buttonSelectInputXMI_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog of = new OpenFileDialog();
			of.Title = "Select xmi file";
			of.Filter = "Xmi files (*.xmi; *.xml)|*.xmi;*.xml|All files (*.*)|*.*";
			of.FilterIndex = 0;
			if(of.ShowDialog() == DialogResult.OK)
			{
				textBoxXmiFilePath.Text = of.FileName;
			}
		}

		private bool increment;
		private void buttonNext_Click(object sender, System.EventArgs e)
		{
			increment = true;
			if(tabControl1.SelectedIndex < tabControl1.TabCount - 1)
				tabControl1.SelectedIndex += 1;
		}

		private void buttonPrev_Click(object sender, System.EventArgs e)
		{
			increment = false;
			if(tabControl1.SelectedIndex > 1)
				tabControl1.SelectedIndex -= 1;
		}

		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(stepsReady)
			{
				if(increment)
					executeAfterStep(selectedTabIndex);

				selectedTabIndex = tabControl1.SelectedIndex;				

				if(increment)
					executeBeforeStep(selectedTabIndex);
			}
		}

		private void executeAfterStep(int i)
		{
			switch(i)
			{
				case 0:
				{
					if(!System.IO.File.Exists(textBoxXmiFilePath.Text))
					{
						MessageBox.Show("You must supply a valid xmi file !");
						tabControl1.SelectedIndex = 0;
					}

					if(!System.IO.Directory.Exists(outputPath))
					{
						System.IO.Directory.CreateDirectory(outputPath);
					}

					if(radioButtonMsSql.Checked)
						DBInput = MsSqlInput;
					else if(radioButtonMySql.Checked)
						DBInput = MySqlInput;

					if(dsProject.Tables.Count == 0)
					{
						createDefaultXMITypeConvertion();
					}
					
					dataGridUMDDataTypes.DataSource = dsProject.Tables["XmiTypeConvertion"];					
				}break;
				case 1:
				{
					string xmiXsltFile = generateXsltXmiFile();
					MakeTransformation(textBoxXmiFilePath.Text, xmiXsltFile);

					foreach(Node node in xmiNode.childNodes)
					{
						string path = System.IO.Path.Combine(outputPath, node.name);
						StreamWriter sw = System.IO.File.CreateText(path);
						sw.Write(node.rtb.Text);
						sw.Close();
						node.path = path;

						DataTable dt = null;
						dt = dsProject.Tables[node.name];
						if(dt == null)
							dt = createDefaultMappingIds(node.name, path);

						DataGrid dg = CreateTabPageMappingIds(node.name);
						dg.DataSource = dt;
					}
				}break;
				case 2:
				{
					if(dsProject.Tables["Code"] == null)
					{						
						createDefaultCodeValues();
					}
					dataGridCodeValues.DataSource = dsProject.Tables["Code"];
					
				}break;
				case 3:
				{
					if(dsProject.Tables["DB"] == null)
					{						
						createDefaultDBValues();
					}
					dataGridDBValues.DataSource = dsProject.Tables["DB"];
				}break;
				case 4:
				{
					labelGenerating.Text = "Generating files";
					mappingsNode = new Node();
					currentNode = mappingsNode;
					foreach(Node node in xmiNode.childNodes)
					{
						defaultNodeName = "Mapping." + node.name;
						string xsltMappingFile = generateXsltMappingFile(node.name);
						MakeTransformation(node.path, xsltMappingFile);
					}
					SaveData(mappingsNode, false);

					codeNode = new Node();
					currentNode = codeNode;
					foreach(Node node in xmiNode.childNodes)
					{
						string xsltCodeFile = generateCodeMappingFile(node.name);
						MakeTransformation(node.path, xsltCodeFile);
					}
					SaveData(codeNode, true);
 
					dbNode = new Node();
					currentNode = dbNode;
					foreach(Node node in xmiNode.childNodes)
					{
						string xsltDBFile = generateDataBaseMappingFile(node.name);
						MakeTransformation(node.path, xsltDBFile);
					}
					SaveData(dbNode, false);

					labelGenerating.Text = "OK";
				}break;
			}
		}

		private void createDefaultCodeValues()
		{
			DataTable dt = new DataTable("Code");
			dsProject.Tables.Add(dt);
				
			dt.Columns.Add("type", typeof(string));
			dt.Columns.Add("value", typeof(string));

			dt.Rows.Add(new object[] {"System.String", "\"\""});
			dt.Rows.Add(new object[] {"System.Boolean", "false"});
			dt.Rows.Add(new object[] {"System.Int32", "0"});
			dt.Rows.Add(new object[] {"System.DateTime", "DateTime.Parse(\"1800-01-01T00:00:00\")"});
			dt.Rows.Add(new object[] {"System.Decimal", "0"});
			dt.Rows.Add(new object[] {"System.Single", "0"});
			dt.Rows.Add(new object[] {"System.Double", "0"});
			dt.Rows.Add(new object[] {"System.Byte[]", "new Byte[0]"});

			foreach(Node node in xmiNode.childNodes)
			{

				XmlDocument doc = new XmlDocument();

				doc.Load(node.path);

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
				nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
				nsmgr.AddNamespace("io", "urn:xmd-io");
				nsmgr.AddNamespace("typing", "urn:xmd-typing");

				foreach(XmlNode xmlnode in doc.SelectNodes("//typing:property/@type", nsmgr))
				{
					if(dt.Select(string.Format("type='{0}'", xmlnode.Value)).Length == 0)
						dt.Rows.Add(new object[] { xmlnode.Value, "0"});
				}
			}
		}

		private void createDefaultDBValues()
		{
			DataTable dt = new DataTable("DB");
			dsProject.Tables.Add(dt);
				
			dt.Columns.Add("type", typeof(string));
			dt.Columns.Add("value", typeof(string));

			dt.Rows.Add(new object[] {"System.String", "<xsl:choose><xsl:when test=\"../@length\">varchar(<xsl:value-of select=\"../@length\"/>) not null default ''</xsl:when><xsl:otherwise>text</xsl:otherwise></xsl:choose>"});
			dt.Rows.Add(new object[] {"System.Boolean", "bit not null default '0'"});
			dt.Rows.Add(new object[] {"System.Int32", "int not null default '0'"});
			dt.Rows.Add(new object[] {"System.DateTime", "datetime not null default '1800-01-01T00:00:00'"});
			dt.Rows.Add(new object[] {"System.Decimal", "float not null default '0'"});
			dt.Rows.Add(new object[] {"System.Single", "float not null default '0'"});
			dt.Rows.Add(new object[] {"System.Double", "float not null default '0'"});
			dt.Rows.Add(new object[] {"System.Byte[]", "image"});

			foreach(Node node in xmiNode.childNodes)
			{
				XmlDocument doc = new XmlDocument();

				doc.Load(node.path);

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
				nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
				nsmgr.AddNamespace("io", "urn:xmd-io");
				nsmgr.AddNamespace("typing", "urn:xmd-typing");

				
				foreach(XmlNode xmlnode in doc.SelectNodes("//typing:property/@type", nsmgr))
				{
					if(dt.Select(string.Format("type='{0}'", xmlnode.Value)).Length == 0)
						dt.Rows.Add(new object[] { xmlnode.Value, "int not null default '0'"});
				}
			}
		}

		private void executeBeforeStep(int i)
		{
		}

		private void createDefaultXMITypeConvertion()
		{
			DataTable dt = new DataTable("XmiTypeConvertion");
			dt.Columns.Add("UMLDataType", typeof(string));
			dt.Columns.Add("value", typeof(string));
			dt.Rows.Add(new object[] { "@name='string'" , "System.String"});
			dt.Rows.Add(new object[] { "@name='String'" , "System.String"});
			dt.Rows.Add(new object[] { "starts-with(@name, 'String[')" , "System.String"});
			dt.Rows.Add(new object[] { "starts-with(@name, 'String(')" , "System.String"});
			dt.Rows.Add(new object[] { "starts-with(@name, 'bool')", "System.Boolean"});
			dt.Rows.Add(new object[] { "starts-with(@name, 'Bool')", "System.Boolean"});
			dt.Rows.Add(new object[] { "starts-with(@name, 'int')", "System.Int32"});
			dt.Rows.Add(new object[] { "starts-with(@name, 'Int')", "System.Int32"});
			dt.Rows.Add(new object[] { "@name='DateTime'", "System.DateTime"});
			dt.Rows.Add(new object[] { "@name='Stream'", "System.Byte[]"});

			createDefaultXMITypeConvertionFromXMI(dt);

			dsProject.Tables.Add(dt);
		}

		private void createDefaultXMITypeConvertionFromXMI(DataTable dt)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			bool appendAnd = false;
			foreach(DataRow dr in dt.Rows)
			{
				if(appendAnd)
					sb.Append(" and ");
				appendAnd = true;

				sb.AppendFormat(" not({0}) ", dr[0]);
			}

			XmlDocument doc = new XmlDocument();
			doc.Load(textBoxXmiFilePath.Text);

			XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
			nsmgr.AddNamespace("UML", "http://org.omg/UML/1.3");
			
			string xPath = string.Concat("//UML:DataType[", sb.ToString(), "]");
			foreach(XmlElement node in doc.SelectNodes(xPath, nsmgr))
			{
				dt.Rows.Add(new object[] { string.Format("@name='{0}'", node.Name) , "System.Int32"});
			}
		}

		private Hashtable htNames = new Hashtable();

		private int getUniqueId(string name, string tablename)
		{
			string key = string.Concat(tablename, "|", name);
			object o = htNames[key];
			int result = 0;
			if(o != null)
				result = (int) o;
			else
			{
				result = htNames.Count + 1;
				htNames[key] = result;
			}	

			return result;
		}

		private DataTable createDefaultMappingIds(string tableName, string pivotFileName)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			DataTable dt = new DataTable(tableName);
			dt.Columns.Add("ClassName", typeof(string));
			dt.Columns.Add("Id", typeof(int));

			dsProject.Tables.Add(dt);

			XmlDocument doc = new XmlDocument();
			doc.Load(pivotFileName);

			XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
			nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
			nsmgr.AddNamespace("io", "urn:xmd-io");
			nsmgr.AddNamespace("typing", "urn:xmd-typing");
			foreach(XmlElement node in doc.SelectNodes("//*[typing:entity]", nsmgr))
			{
				dt.Rows.Add(new object[] { node.Name, getUniqueId(node.Name, tableName)});
			}

			return dt;
		}

		private void generateXsltFiles()
		{
			XsltXmiFile = generateXsltXmiFile();
		}

		private string XsltXmiFile;

		private string generateXsltXmiFile()
		{
			System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.Combine(Application.StartupPath, XMIInput));
			string fullDoc = sr.ReadToEnd();


			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			string[] strs = System.Text.RegularExpressions.Regex.Split(fullDoc, "(?:<UMLDataType>)");
			sb.Append(strs[0]);

			DataTable dt = dsProject.Tables["XmiTypeConvertion"];

			foreach(DataRow dr in dt.Rows)
			{
				sb.AppendFormat("<xsl:template match=\"UML:DataType[{0}]\">{1}</xsl:template>\n", dr[0], dr[1]);
			}

			strs = System.Text.RegularExpressions.Regex.Split(fullDoc, "(?:</UMLDataType>)");
			sb.Append(strs[1]);
			string result = System.IO.Path.Combine(outputPath, "XMI.xslt");
			System.IO.StreamWriter sw = System.IO.File.CreateText(result);
			sw.Write(sb.ToString());
			sw.Close();
			return result;
		}

		private string generateXsltMappingFile(string tableName)
		{
			System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.Combine(Application.StartupPath, MapInput));
			string fullDoc = sr.ReadToEnd();			

			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			string[] strs = System.Text.RegularExpressions.Regex.Split(fullDoc, "<MapInput/>");
			sb.Append(strs[0]);

			DataTable dt = dsProject.Tables[tableName];

			foreach(DataRow dr in dt.Rows)
			{
				//
				sb.AppendFormat("<xsl:when test=\"local-name($classNode) = '{0}'\">{1}</xsl:when>\n", dr[0], dr[1]);
			}

			sb.Append(strs[1]);
			string result = System.IO.Path.Combine(outputPath, "Mapping." + tableName + ".xslt");
			System.IO.StreamWriter sw = System.IO.File.CreateText(result);
			sw.Write(sb.ToString());
			sw.Close();
			return result;
		}

		private string generateCodeMappingFile(string tableName)
		{
			System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.Combine(Application.StartupPath, CSharpInput));
			string fullDoc = sr.ReadToEnd();			

			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			string[] strs = System.Text.RegularExpressions.Regex.Split(fullDoc, "<CSharpInput/>");
			sb.Append(strs[0]);

			DataTable dt = dsProject.Tables["Code"];

			foreach(DataRow dr in dt.Rows)
			{
				sb.AppendFormat("<xsl:template match=\"@type[.='{0}']\">{1}</xsl:template>\n", dr[0], dr[1]);
			}

			sb.Append(strs[1]);
			string result = System.IO.Path.Combine(outputPath, "CSharp." + tableName + ".xslt");
			System.IO.StreamWriter sw = System.IO.File.CreateText(result);
			sw.Write(sb.ToString());
			sw.Close();
			return result;
		}

		private string generateDataBaseMappingFile(string tableName)
		{
			System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.Combine(Application.StartupPath, DBInput));
			string fullDoc = sr.ReadToEnd();			

			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			string[] strs = System.Text.RegularExpressions.Regex.Split(fullDoc, "<DBInput/>");
			sb.Append(strs[0]);

			DataTable dt = dsProject.Tables["DB"];

			foreach(DataRow dr in dt.Rows)
			{
				sb.AppendFormat("<xsl:template match=\"@type[.='{0}']\">{1}</xsl:template>\n", dr[0], dr[1]);
			}

			sb.Append(strs[1]);
			string result = System.IO.Path.Combine(outputPath, "DBInput." + tableName + ".xslt");
			System.IO.StreamWriter sw = System.IO.File.CreateText(result);
			sw.Write(sb.ToString());
			sw.Close();
			return result;
		}

		#region IXmlWriterProvider Members
		
		private Node currentNode = null;
		private string defaultNodeName = "";

		XmlWriter Eldorado.Metadata.IXmlWriterProvider.GetWriter(string name)
		{
			
			if (name == null || name == "")
				name = defaultNodeName;			

			Eldorado.Metadata.XmlRichTextBoxWriter writer = new Eldorado.Metadata.XmlRichTextBoxWriter(CreateTabPage(name));
			addNode(name, writer.RichTextBox);
			return writer;
		}

		private void addNode(string nodename, RichTextBox rtb)
		{
			Node node = new Node();
			currentNode.childNodes.Add(node);
			node.parent = currentNode;
			node.name = nodename;
			node.path = System.IO.Path.Combine(outputPath, nodename);
			node.rtb = rtb;

		}

		#endregion

		public RichTextBox CreateTabPage(string Caption)
		{
			TabPage tabPageResult = new System.Windows.Forms.TabPage();			
			RichTextBox richTextBoxResult = new System.Windows.Forms.RichTextBox();
			// 
			// tabPageResult
			// 
			tabPageResult.AutoScroll = true;
			tabPageResult.Controls.Add(richTextBoxResult);
			tabPageResult.Location = new System.Drawing.Point(4, 22);
			tabPageResult.Name = "tabPageResult";
			tabPageResult.Size = new System.Drawing.Size(544, 359);
			tabPageResult.TabIndex = 0;
			tabPageResult.Text = Caption;
			// 
			// richTextBoxResult
			// 
			richTextBoxResult.Dock = System.Windows.Forms.DockStyle.Fill;
			richTextBoxResult.Location = new System.Drawing.Point(0, 0);
			richTextBoxResult.Name = "richTextBoxResult";
			richTextBoxResult.Size = new System.Drawing.Size(544, 359);
			richTextBoxResult.TabIndex = 0;
			richTextBoxResult.Text = "";
			// 
			// tabControl1
			// 
			results.TabControl.Controls.Add(tabPageResult);

			return richTextBoxResult;
		}	

		public DataGrid CreateTabPageMappingIds(string Caption)
		{
			TabPage tabPageResult = new System.Windows.Forms.TabPage();
			DataGrid dg = new DataGrid();
			// 
			// tabPageResult
			// 
			tabPageResult.AutoScroll = true;
			tabPageResult.Controls.Add(dg);
			tabPageResult.Location = new System.Drawing.Point(4, 22);
			tabPageResult.Name = "tabPageResult";
			tabPageResult.Size = new System.Drawing.Size(544, 359);
			tabPageResult.TabIndex = 0;
			tabPageResult.Text = Caption;
			// 
			// dg
			// 
			dg.Dock = System.Windows.Forms.DockStyle.Fill;
			// 
			// tabControl1
			// 
			tabControlMapping.Controls.Add(tabPageResult);

			return dg;
		}

		public void MakeTransformation(string xmlFile, string xslFile)
		{
			try
			{
				this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
				
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(xmlFile);
				XmlDocument xslDocument = new XmlDocument();
				xslDocument.Load(xslFile);
				XslTransform xslTransform = new XslTransform();
				//xslTransform.Load(xslDocument/*, null, null*/);
				xslTransform.Load(xslDocument, null, this.GetType().Assembly.Evidence);
				StringBuilder sb = new StringBuilder();

				XsltArgumentList args = new XsltArgumentList();
				foreach(XmlNode pi in xslDocument.SelectNodes("descendant::processing-instruction()[local-name()='arg']"))
				{
					int pos = pi.Value.IndexOf("=");
					if (pos == -1)
						throw new Exception("processing instruction format error: expecting \"<?arg namespace=type ?>\"");
					string ns = pi.Value.Substring(0, pos).Trim();
					string typeName = pi.Value.Substring(pos + 1).Trim();
					Type type = Type.GetType(typeName);
					if (type == null)
						throw new Exception("type " + typeName + " not found");
					object arg = type.GetConstructor(new Type[]{}).Invoke(null);
					args.AddExtensionObject(ns, arg);
				}

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
				nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
				nsmgr.AddNamespace("io", "urn:xmd-io");
				int idx = 0;
				if (xslDocument.SelectSingleNode("/xsl:stylesheet/xsl:output[@method='text']", nsmgr) != null)
				{
					xslTransform.Transform(xmlDocument, args, new StringWriter(sb), null);
					string result = sb.ToString();
					RichTextBox richTextBoxResult = null;
					while (result != "")
					{
						int i = result.IndexOf("\n");
						string line;
						if (i == -1)
						{
							line = result;
							result = "";
						}
						else
						{
							line = result.Substring(0, i + 1);
							result = result.Substring(i + 1);
						}
						if (line.StartsWith("---- File: "))
						{
							string fileName = line.Substring("---- File: ".Length);
							
							if (fileName.EndsWith("\n"))
								fileName = fileName.Substring(0, fileName.Length - 1);
							richTextBoxResult = CreateTabPage(fileName);
							addNode(fileName, richTextBoxResult);
							continue;
						}

						if (richTextBoxResult == null)
						{
							idx++;
							richTextBoxResult = CreateTabPage("Result");
							addNode("Result" + idx, richTextBoxResult);
						}

						richTextBoxResult.Text += line;
					}
				}
				else 
				{

					XmlWriter xmlWriter = new Eldorado.Metadata.XmlMultiFileWriter(this);
					//xslTransform.Transform(xmlDocument, args, xmlWriter/*, null*/);
					xslTransform.Transform(xmlDocument, args, xmlWriter, null);					
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.StackTrace);
				while (ex != null) 
				{
					MessageBox.Show(ex.Message);
					ex = ex.InnerException;
				}
			}
			finally
			{
				this.Cursor = System.Windows.Forms.Cursors.Default;
			}
		}

		public class Node
		{
			public RichTextBox rtb;
			public Node parent;
			public ArrayList childNodes = new ArrayList();
			public string name;
			public string path;
		}

		public void SaveData(Node node, bool merge)
		{
			ArrayList notGeneratedFiles = new ArrayList(node.childNodes.Count);

			foreach(Node childNode in node.childNodes )
			{
				string fileName = childNode.path;
				fileName = fileName.Replace(".DataAccess.DataAccess",".DataAccess");
				
				fileName = System.IO.Path.Combine(outputPath, fileName);
				System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fileName));

				RichTextBox richTextBox = childNode.rtb;
				try
				{
					if(merge && System.IO.File.Exists(fileName))
						MergeData(richTextBox.Text, fileName);
					else
					{
						System.IO.StreamWriter sw = System.IO.File.CreateText(fileName);
						sw.Write(richTextBox.Text);
						sw.Close();
					}						
				}
				catch(System.UnauthorizedAccessException)
				{
					notGeneratedFiles.Add(fileName);
				}
			}

			if(notGeneratedFiles.Count != 0)
			{
				System.Text.StringBuilder sb = new StringBuilder();
				foreach(string s in notGeneratedFiles)
					sb.AppendFormat("{0}\n", s);

				MessageBox.Show(null, 
					string.Concat("Generation failed for the folowing files : \n", sb.ToString()),
					"Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		public void MergeData(string text, string fileName)
		{
			FileStream fs = File.OpenRead(fileName);
			StreamReader sr = new StreamReader(fs);
			string existingFile = sr.ReadToEnd();
			string newFile = text;
			
			Regex regexExisting = new Regex(
				@"(?<region>\#region\s(?<def>user\scode\s[^\n]*))",
				RegexOptions.IgnoreCase | RegexOptions.Multiline 
				| RegexOptions.IgnorePatternWhitespace  | RegexOptions.ExplicitCapture 
				| RegexOptions.Compiled);	

			MatchCollection matchFcts = regexExisting.Matches(existingFile);
			foreach(Match match in matchFcts)
			{
				string def = match.Groups["def"].Value;
				string endRegion = string.Concat("#endregion ", def).Trim();
				def = Regex.Escape(def.Trim());
				int endExisting = existingFile.IndexOf(endRegion, match.Index);
				if(endExisting >= 0)
				{				
					Regex regexNewFile = new Regex(
						string.Concat(@"(?<region>\#region\s", def ,")",
						@"(?<code>[^#]*)(?<endregion-region>\#endregion\s", def, @"(?<endspace>\s))"),
						RegexOptions.IgnoreCase | RegexOptions.Multiline 
						| RegexOptions.IgnorePatternWhitespace  | RegexOptions.ExplicitCapture 
						| RegexOptions.Compiled | RegexOptions.CultureInvariant);
					newFile = regexNewFile.Replace(newFile, string.Concat(existingFile.Substring(match.Index, endExisting + endRegion.Length - match.Index), "${endspace}"));
				}
			}
			sr.Close();
			fs.Close();
			StreamWriter sw = File.CreateText(fileName);
			sw.Write(newFile);
			sw.Close();
		}

		private static string ExtractFilePath(string fileName)
		{
			int pos = fileName.LastIndexOf("\\");
			if (pos == -1)
				return "";
			return fileName.Substring(0, pos);
		}

		private static void ForceDirectory(string fileName)
		{
			if (fileName == "")
				return;
			System.IO.Directory.CreateDirectory(fileName);
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			results.Show();		
		}

		private void buttonSave_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog sd = new SaveFileDialog();
			sd.Title = "Save project";
			sd.DefaultExt = ".eldwzd";
			sd.Filter = "Eldorado project wizard (*.eldwzd)|*.eldwzd";
			if(sd.ShowDialog() == DialogResult.OK)
			{
				Hashtable htSave = new Hashtable();
				htSave["Dataset"] = dsProject;
				htSave["input"] = textBoxXmiFilePath.Text;
				htSave["output"] = outputPath;
				htSave["MsSql"] = radioButtonMsSql.Checked;
				htSave["MySql"] = radioButtonMySql.Checked;

				System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
				sf.Serialize(System.IO.File.OpenWrite(sd.FileName), htSave);				
			}
		}

		private void buttonChooseOutput_Click(object sender, System.EventArgs e)
		{
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			if(fbd.ShowDialog() == DialogResult.OK)
			{
				textBoxOutput.Text = fbd.SelectedPath;
			}
		}

		private void textBoxOutput_TextChanged(object sender, System.EventArgs e)
		{
			outputPath = textBoxOutput.Text;
		}

		private void buttonLoadProject_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Title = "Open project";
			ofd.DefaultExt = ".eldwzd";
			ofd.Filter = "Eldorado project wizard (*.eldwzd)|*.eldwzd";
			if(ofd.ShowDialog() == DialogResult.OK)
			{
				System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
				Hashtable htProject = sf.Deserialize(System.IO.File.OpenRead(ofd.FileName)) as Hashtable;
				
				dsProject = htProject["Dataset"] as DataSet;
				textBoxXmiFilePath.Text = (string) htProject["input"];
				textBoxOutput.Text = (string) htProject["output"];
				radioButtonMsSql.Checked = (bool) htProject["MsSql"];
				radioButtonMySql.Checked = (bool) htProject["MySql"];
			}
		}
	}
}